package BaseObject;

public class Itinerary2 extends Itinerary{

	public Itinerary2(Client c1, Client c2) {
		super(c1,c2);
	}
	
	public boolean computeItinerary(double base)
	{
		double probaC1 = Tools.N(15.0,5.0, Motor.currentTime - this.clientItinerary.get(0).getAppearingTime() + base * Motor.stepTime );
		double probaC2 = Tools.N(15.0,5.0, Motor.currentTime - this.clientItinerary.get(1).getAppearingTime() + ( base + this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(1).getPosition()) ) * Motor.stepTime );
		probaC1 = 1 - probaC1;
		probaC2 = 1 - probaC2;
		
		double probaC1ANDC2 = probaC1 *probaC2 ;
		double probaC2C1 = probaC1ANDC2 / probaC1;
		this.probability = probaC2C1;
		
		//Compute the minimum length for client's rides
		double minLengthC1 = this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(0).getDestination());
		
		//C1's way
		this.length = this.clientItinerary.get(0).getPosition().distance(this.clientItinerary.get(1).getPosition());
		this.length += this.clientItinerary.get(1).getPosition().distance(this.clientItinerary.get(1).getDestination());
		this.length += this.clientItinerary.get(1).getDestination().distance(this.clientItinerary.get(0).getDestination());
		
		//Verify the threeshold condition
		
		if(minLengthC1 * (1 + this.clientItinerary.get(0).getMaxDeviationThreeshold()) < this.length)
			return false;
		
		this.length = this.length - base;
		
		return true;
	}

	
	public void moveThisTaxi(Taxi t)
	{
		boolean change=false;
		if(t.getTekenClientList().size() == 2 )
		{
			
			change = t.deplacerTaxi(t.getTekenClientList().get(1).getDestination());
			//si l'on depose le client 1
			if(change)
			{
				Client c= t.getTekenClientList().get(1);
				t.getTekenClientList().remove(1);
				Motor.clientsTaken.remove(c); 
				
			}
		}
		else if(t.getTekenClientList().size()==1 && t.getItinerary() != null)
		{
			//S'il a le premier client
			if(t.getTekenClientList().get(0).equals(t.getItinerary().clientItinerary.get(0)))
			{
				//Si il na pas encore pris le client 2
				if(Motor.clients.contains(t.getItinerary().clientItinerary.get(1)))
				{
					Client c2=t.getItinerary().clientItinerary.get(1);
					change = t.deplacerTaxi(c2.getPosition());
					if(change) 	// Si l'on arrive sur le client
					{
						Motor.clients.remove(c2);
						Motor.clientsTaken.add(c2);
						t.getTekenClientList().add(c2);
						Motor.increaseNbClientDone();
					}
				}
				else
				{
					Itinerary4 it = new Itinerary4(t.getItinerary().clientItinerary.get(0), null);
					t.setItinerary(it);
					
				}
			}
		}
	}
}
